package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 主题内容数据传输对象
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "主题内容信息")
public class ContentDTO {

    @NotNull(message = "内容ID不能为空")
    @Schema(description = "内容唯一标识", example = "1")
    private Long id;

    @NotBlank(message = "内容标题不能为空")
    @Size(max = 200, message = "内容标题长度不能超过200个字符")
    @Schema(description = "内容标题", example = "Java编程入门指南")
    private String title;

    @Size(max = 1000, message = "内容描述长度不能超过1000个字符")
    @Schema(description = "内容简要描述", example = "详细介绍Java编程语言的基础知识和实践技巧")
    private String description;

    @Size(max = 500, message = "封面图URL长度不能超过500个字符")
    @Schema(description = "内容封面图URL", example = "https://example.com/cover.jpg")
    private String coverImage;

    @Schema(description = "关联主题ID", example = "1")
    private Long themeId;

    @Schema(description = "关联主题名称", example = "编程基础")
    private String themeName;

    @Size(max = 50, message = "内容类型长度不能超过50个字符")
    @Schema(description = "内容类型", example = "文章")
    private String contentType;

    @Min(value = 0, message = "排序顺序不能为负数")
    @Schema(description = "排序顺序", example = "1")
    private Integer sortOrder;

    @NotNull(message = "发布状态不能为空")
    @Schema(description = "是否已发布", example = "true")
    private Boolean isPublished;

    /**
     * 从实体类转换为DTO
     *
     * @param entity 主题内容实体
     * @return 主题内容DTO
     */
    public static ContentDTO fromEntity(com.example.teaching_server.entity.ThemeContent entity) {
        if (entity == null) {
            return null;
        }

        return ContentDTO.builder()
                .id(entity.getId())
                .title(entity.getTitle())
                .description(entity.getDescription())
                .coverImage(entity.getCoverImage())
                .themeId(entity.getTheme() != null ? entity.getTheme().getId() : null)
                .themeName(entity.getTheme() != null ? entity.getTheme().getName() : null)
                .contentType(entity.getContentType())
                .sortOrder(entity.getSortOrder())
                .isPublished(entity.getIsPublished())
                .build();
    }

    /**
     * 获取内容摘要
     *
     * @return 内容摘要
     */
    @Schema(description = "内容摘要", accessMode = Schema.AccessMode.READ_ONLY)
    public String getSummary() {
        if (description != null && !description.trim().isEmpty()) {
            return description.length() > 100 ?
                    description.substring(0, 100) + "..." : description;
        }
        return title != null && title.length() > 100 ?
                title.substring(0, 100) + "..." : title;
    }

    /**
     * 检查内容是否有效
     *
     * @return 是否有效
     */
    @Schema(description = "内容是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public Boolean isValid() {
        return title != null && !title.trim().isEmpty() &&
                themeId != null;
    }
}
